package com.kintreda.ieco_server.module.order.service.impl;

import com.kintreda.ieco_server.bean.account.User;
import com.kintreda.ieco_server.bean.goods.Items;
import com.kintreda.ieco_server.bean.goods.Sack;
import com.kintreda.ieco_server.bean.goods.Waste;
import com.kintreda.ieco_server.bean.order.RecycleRecord;
import com.kintreda.ieco_server.bean.order.Unsuitable;
import com.kintreda.ieco_server.bean.order.UsageRecord;
import com.kintreda.ieco_server.bean.user.Statistics;
import com.kintreda.ieco_server.core.repository.IBaseRepository;
import com.kintreda.ieco_server.core.service.impl.BaseServiceImpl;
import com.kintreda.ieco_server.core.web.R;
import com.kintreda.ieco_server.module.account.service.IUserService;
import com.kintreda.ieco_server.module.order.service.*;
import com.kintreda.ieco_server.util.Parameters;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @Created by IntelliJ IDEA
 * @Project_Name ieco_server
 * @Package com.kintreda.ieco_server.module.order.service.impl
 * @Author ZhongYao_H
 * @Date 2019/4/28:17:10
 * @Year 2019 Month 04 Day 28
 * @Date_zh 2019 年 四月 星期日
 */
@Service
public class RecycleRecordServiceImpl extends BaseServiceImpl<RecycleRecord,String> implements IRecycleRecordService {


    @Autowired
    private IUsageRecordService usageRecordService;
    @Autowired
    private IStatisticsService statisticsService;
    @Autowired
    private IRecycleRecordService recycleRecordService;
    @Autowired
    private IWasteService wasteService;
    @Autowired
    private IRegenerantService regenerantService;
    @Autowired
    private IUnsuitableService unsuitableService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IBaseRepository<RecycleRecord,String> recycleRecordRepository;
    @Autowired
    private ISackService sackService;



    @Override
    public synchronized R close(String[] type,String[] ids, String[] num, String id,String differentiate,Integer rate,String explain) {

        UsageRecord usageRecord = usageRecordService.get(id);

        if (StringUtils.equals(usageRecord.getStatus(),"1")){
            return R.error("订单已结算!");
        }
        Sack  sack  =  sackService.getSackByQrCode(usageRecord.getQrCode());
        sack.setStatus("0");
        User  user  = userService.get(usageRecord.getUserOrOrgid());
        Statistics statistics ;
        if (user.getType().equals("4") || user.getType().equals("3")){
            statistics = statisticsService.getByUserIdAndType(user.getId(),"1");
        }else {
            statistics = statisticsService.getByUserIdAndType(user.getId(),"0");
        }

            // 结算处理i逻辑
                    if (usageRecord!=null){

                        //如果获取不到用户的统计信息 则是用户是第一次结算 创建用户的统计信息
                        if (statistics==null){
                            statistics = new Statistics();
                    statistics.setUserId(user.getId());
                    if (user.getType().equals("4") || user.getType().equals("3")){
                        statistics.setType("1");
                    }else {
                        statistics.setType("0");
                    }
                    statisticsService.save(statistics);
                }


                //交易记录
                RecycleRecord recycleRecord = new RecycleRecord();
                recycleRecord.setUserId(usageRecord.getUserOrOrgid());
                recycleRecord.setSackId(usageRecord.getQrCode());
                recycleRecord.setOrgId(user.getOrgId());
                recycleRecord.setSackCode(sack.getQrcode());
                String  recycleId = recycleRecordService.save(recycleRecord).toString();
                //物品总计
                Map<String, BigDecimal> result = waste(type,ids, num, recycleId);
                //交易记录设置 此次交易金额 和物品总重

                //当前余额
                BigDecimal balance  = statistics.getBalance();
                //此次结算金额 (扣除收益后的)
                BigDecimal thistotal = new BigDecimal(0);
                //扣除金额
                BigDecimal sub = new BigDecimal(0);
                if (StringUtils.equals("0",differentiate)){
                    //无不当行为

                    //最终结算金额
                    thistotal = result.get("sum");
                    //设置余额

                    //记录不当行为
                    Unsuitable  unsuitable = new Unsuitable();
                    unsuitable.setUserId(user.getId());
                    unsuitable.setRecyId(recycleId);
                    unsuitable.setAnno("");
                    unsuitable.setStatus("0");
                    unsuitable.setRate("0%");
                    unsuitable.setTotal("0");
                    unsuitableService.save(unsuitable);
                }else {
                    //有不当行为  不指定减少收益默认 减少5%
                    if (rate==null){
                        //默认减少 5%收益
                        rate = Parameters.RATE;
                    }
                    BigDecimal number = new BigDecimal(100);
                    BigDecimal r = new BigDecimal(rate);
                    //扣除收益率
                    BigDecimal divide = r.divide(number);
                    //未扣除收益的结算金额
                    BigDecimal total = result.get("sum");
                    //扣除的金额
                    BigDecimal multiply = divide.multiply(total);
                    sub = multiply;
                    //减去扣除的金额
                    total = total.subtract(multiply);
                    //最终结算金额
                    thistotal =  total;

                    //记录不当行为
                    Unsuitable  unsuitable = new Unsuitable();
                    unsuitable.setUserId(user.getId());
                    unsuitable.setRecyId(recycleId);
                    unsuitable.setAnno(explain);
                    unsuitable.setStatus("1");
                    unsuitable.setRate(rate+"%");
                    unsuitable.setTotal(multiply+"");
                    unsuitableService.save(unsuitable);


                }


                //设置余额
                balance = thistotal.add(balance);
                statistics.setBalance(balance);

                recycleRecord.setTotal(thistotal+"");
                recycleRecord.setGross(result.get("gross")+"");
                recycleRecordService.update(recycleRecord);
                statisticsService.saveOrUpdate(statistics);
                sackService.update(sack);
                usageRecord.setStatus("1");
                usageRecordService.update(usageRecord);
                return  R.ok("处理成功,总价为:"+thistotal.toString()+"元,减少收益"+sub.toString()+"元");
            }
        return R.error("处理结算 计算时发送错误!");
    }


    @Override
    public Map<String,BigDecimal> waste(String[] type,String[] ids, String[] num, String recycleId) {

        List<Waste> wastes = new LinkedList<>();
        BigDecimal sum = new BigDecimal(0);
        BigDecimal gross = new BigDecimal(0);//所有物品总重
        for (int i=0;i<type.length;i++) {
            Items items = regenerantService.get(type[i]);
            Items goods = regenerantService.get(ids[i]);
            Waste waste = new Waste();
            waste.setSackIrecycleRecord(recycleId);
            waste.setNum(new BigDecimal(num[i]));
            waste.setTypeId(items.getId());
            BigDecimal price;
            if (items.getTypeB().equals("1")){
                waste.setPrice(new BigDecimal(goods.getMoney()));
                price = new BigDecimal(goods.getMoney());
            }else {
                waste.setPrice(new BigDecimal(items.getMoney()));
                price = new BigDecimal(items.getMoney());
            }
            waste.setName(items.getName());
            //金额
            BigDecimal n = new BigDecimal(num[i]);

            BigDecimal s = n.multiply(price);
            BigDecimal add = sum.add(s);
            sum = new BigDecimal(add.toString());

            //总重
            BigDecimal gro = gross.add(n);
            gross = gro;

            wastes.add(waste);
        }
        wasteService.saveOrUpdateAll(wastes);
        Map<String,BigDecimal> r = new HashMap<>();
        r.put("sum",sum);
        r.put("gross",gross);
        return r;
    }

    @Override
    public List<Map<String,Object>> getMyRecycleRecord(String userid) {
        List<Map<String, Object>> listBySql = recycleRecordRepository.findListBySql("select t1.*,t2.anno,t2.rate,t2.total as total2 from recyclerecord t1,unsuitable t2 where t1.id = t2.recyId and t1.userId = ? order by createDate desc limit 0,50 ", new Object[]{userid});
//        return query("from RecycleRecord where userId = ? order by createDate desc",new Object[]{userid});
    return listBySql;
    }

    @Override
    public Long count(String userId) {
        Long count = recycleRecordRepository.countBySql(" select count(*) from recyclerecord where userId = ? ", new String[]{userId});
        return count;
    }

    @Override
    public Long countIncome(String userId) {
        JdbcTemplate jdbcTemplate = recycleRecordRepository.getJdbcTemplate();
        List<Long> longs = jdbcTemplate.queryForList("select sum(total) from recyclerecord where userId = ?", new Object[]{userId}, Long.class);
        try {
            Long s = longs.get(0);
            return s;
        }catch (Exception e){
            return 0L;
        }

    }

    @Override
    public List<RecycleRecord> getListByCode(String code) {
        return query("from RecycleRecord where sackId = ?",new Object[]{code});
    }

    @Override
    public List<RecycleRecord> getListByUserId(String userId) {
        return query("from RecycleRecord where userId = ?",new Object[]{userId});

    }

    @Override
    public List<Map<String,Object>> getOrgListByOrgId(String orgId) {

        //查询机构回收记录
        List<Map<String, Object>> recycleRecordslist = recycleRecordRepository.findListBySql("SELECT * from recyclerecord where userId in (SELECT id from common_user where orgId= ? ) ORDER BY createDate desc ", new Object[]{orgId});
            for (Map<String, Object> recy : recycleRecordslist) {

                List<Waste> waste = wasteService.findByRecyId(recy.get("id").toString());
                if (waste!=null){
                    recy.put("waste",waste);
                }
            }

        return recycleRecordslist;
    }

    @Override
    public List<Map<String, Object>> getOrgListByUserId(String orgId) {
        //查询个人回收记录
        List<Map<String, Object>> list = recycleRecordRepository.findListBySql("select adminId from organization where  id = ? ", new Object[]{orgId});
        List<Map<String,Object>> recycleRecordslist =  new LinkedList();
        for (Map<String, Object> map : list) {
            List<Map<String, Object>> recys = recycleRecordRepository.findListBySql("select * from recyclerecord where userId = ? order by createDate desc limit 0 , 30 ", new Object[]{map.get("adminId").toString()});
            for (Map<String, Object> recy : recys) {
                List<Waste> waste = wasteService.findByRecyId(recy.get("id").toString());
                if (waste!=null){
                    recy.put("waste",waste);
                }
                recycleRecordslist.add(recy);

            }
        }

        return recycleRecordslist;
    }

    @Override
    public List<Map<String, Object>> getAll() {
        return recycleRecordRepository.findListBySql(" select id, sackId,total,gross,createDate from recyclerecord ",null);
    }

    @Override
    public Long countCloseByOrgId(String orgId) {
        List<Map<String, Object>> conut = recycleRecordRepository.findListBySql("select count(*) as count from recyclerecord where userId in (SELECT id from common_user where orgId = ?)", new Object[]{orgId});
        try {
            return Long.parseLong(conut.get(0).get("count").toString());
        }catch (Exception e){
            return 0L;
        }
    }

    @Override
    public Long countCloseByUserId(String userId) {
        List<Map<String, Object>> conut = recycleRecordRepository.findListBySql("select count(*) as count from recyclerecord where userId = ? ", new Object[]{userId});
        try {
            return Long.parseLong(conut.get(0).get("count").toString());
        }catch (Exception e){
            return 0L;
        }
    }

    /**
     *
     * 计算机构环保值 TODO
     *
     * @param orgId
     * @return
     */
    @Override
    public Map<String, Object> ePValue(String orgId) {
        /*计算机构环保值
         * 1 根据机构id查询出所有机构的用户
         * 2 根据每个用户id查询所有回收记录
         * 3 根据每条回收记录的id查询回收的每种物品
         * 4 记录每种物品的总数
         * 5 每种物品根据提供的计算比例计算每种环保
         * 6 把每种物品的环保相加
         * */

        /*环保值
         *
         * tree 树(课)
         * oil  石油(吨)
         * carbonDioxide 二氧化碳 (吨)
         * land 土地
         * */
        Double tree = new Double(0.0);
        Double oil = new Double(0.0);
        Double carbonDioxide = new Double(0.0);
        Double land = new Double(0.0);


        List<Map<String, Object>> userList = recycleRecordRepository.findListBySql("select id from common_user where orgId = ? ", new Object[]{orgId});

        /*所有类型*/
        List<Items> itemsList = regenerantService.getType("1");

        for (Map<String, Object> map : userList) {

            String userId = map.get("id").toString();
            /*用户的回收记录Id列表*/
            List<RecycleRecord> recycleRecordList = recycleRecordService.getListByUserId(userId);

            for (RecycleRecord recy : recycleRecordList) {

                List<Waste> wastes = wasteService.getWasteByRecyId(recy.getId());

                for (Waste waste : wastes) {
                    String typeId = waste.getTypeId();
                    Items type = regenerantService.get(typeId);
                    Double weight = waste.getNum().doubleValue() / 1000;
                    Double tree_said;
                    Double oil_said;
                    Double carbonDioxide_said;
                    Double land_said;


                    if (type != null) {
                        for (Items items : itemsList) {
                            if (items.getId().equals(type.getId())) {

                            /* 类型  0 PET瓶 1 PE瓶 2 泡沫 3 硬质塑料 4 塑料袋膜 5 织物 6 玻璃
                                    7 复合 8 电子电器 9 书报  10 综合纸 11 金属 12 铝拉罐 13 黄纸板
                             */

                                switch (items.getTyepC()) {
                                    case "0":

                                        /*树*/
                                        //树不是PET塑料做的

                                        /*石油*/
                                        oil_said = weight * 2.3;
                                        oil += oil_said;

                                        /*二氧化碳*/
                                        carbonDioxide_said = weight * 2.8;
                                        carbonDioxide += carbonDioxide_said;

                                        /*土地*/
                                        land_said = weight * 23.0;
                                        land += land_said;
                                        break;
                                    case "1":
                                        /*树*/
                                        //树不是PE塑料做的

                                        /*石油*/
                                        oil_said = weight * 2.3;
                                        oil += oil_said;

                                        /*二氧化碳*/
                                        carbonDioxide_said = weight * 2.8;
                                        carbonDioxide += carbonDioxide_said;


                                        /*土地*/
                                        land_said = weight * 23.0;
                                        land += land_said;

                                        break;
                                    case "2":
                                        /*树*/
                                        //树不是泡沫塑料做的

                                        /*石油*/
                                        oil_said = weight * 2.3;
                                        oil += oil_said;

                                        /*二氧化碳*/
                                        carbonDioxide_said = weight * 2.8;
                                        carbonDioxide += carbonDioxide_said;


                                        /*土地*/
                                        land_said = weight * 23.0;
                                        land += land_said;
                                        break;
                                    case "3":
                                        /*树*/
                                        //树不是硬质塑料做的

                                        /*石油*/
                                        oil_said = weight * 2.3;
                                        oil += oil_said;

                                        /*二氧化碳*/
                                        carbonDioxide_said = weight * 2.8;
                                        carbonDioxide += carbonDioxide_said;


                                        /*土地*/
                                        land_said = weight * 23.0;
                                        land += land_said;
                                        break;
                                    case "4":

                                        /*树*/
                                        //树不是塑料袋膜做的

                                        /*石油*/
                                        oil_said = weight * 2.3;
                                        oil += oil_said;

                                        /*二氧化碳*/
                                        carbonDioxide_said = weight * 2.8;
                                        carbonDioxide += carbonDioxide_said;


                                        /*土地*/
                                        land_said = weight * 23.0;
                                        land += land_said;


                                        break;
                                    case "5":

                                        /*树*/

                                        /*石油*/

                                        /*二氧化碳*/
                                        carbonDioxide_said = weight * 5.9;
                                        carbonDioxide += carbonDioxide_said;

                                        /*土地*/

                                        break;
                                    case "6":
                                        /*树*/

                                        /*石油*/
                                        oil_said = weight * 0.016;
                                        oil += oil_said;

                                        /*二氧化碳*/
                                        carbonDioxide_said = weight * 0.0034;
                                        carbonDioxide += carbonDioxide_said;

                                        /*土地*/

                                        land_said = weight * 1.53;
                                        land += land_said;

                                        break;
                                    case "7":
                                        /*树*/

                                        /*石油*/
                                        oil_said = weight * 1.68;
                                        oil += oil_said;

                                        /*二氧化碳*/
                                        carbonDioxide_said = weight * 2.04;
                                        carbonDioxide += carbonDioxide_said;

                                        /*土地*/

                                        land_said = weight * 1.53;
                                        land += land_said;
                                        break;
                                    case "8":
                                        /*树*/
                                        Double tree_avg = 17.0 / 14.0;
                                        tree_said = weight * tree_avg;
                                        tree += tree_said;
                                        /*石油*/
                                        Double avg_oil = (2.3 + 0.016 + 1.68 + 1.2 + 5.7) / 14;
                                        oil_said = weight * avg_oil;
                                        oil += oil_said;

                                        /*二氧化碳*/
                                        Double avg_carbonDioxide = (2.8 + 5.9 + 0.0034 + 2.04 + 1.52 + 5.5) / 14;
                                        carbonDioxide_said = weight * avg_carbonDioxide;
                                        carbonDioxide += carbonDioxide_said;

                                        /*土地*/
                                        Double land_avg = (23 + 1.53 + 7.17 + 2.5 + 7.6) / 14;
                                        land_said = weight * land_avg;
                                        land += land_said;


                                        break;
                                    case "9":
                                        /*树*/
                                        tree_said = weight * 17;
                                        tree += tree_said;

                                        /*石油*/
                                        oil_said = weight * 1.2;
                                        oil += oil_said;

                                        /*二氧化碳*/
                                        carbonDioxide_said = weight * 1.52;
                                        carbonDioxide += carbonDioxide_said;

                                        /*土地*/

                                        land_said = weight * 2.5;
                                        land += land_said;
                                        break;
                                    case "10":
                                        /*树*/
                                        tree_said = weight * 17;
                                        tree += tree_said;

                                        /*石油*/
                                        oil_said = weight * 1.2;
                                        oil += oil_said;

                                        /*二氧化碳*/
                                        carbonDioxide_said = weight * 1.52;
                                        carbonDioxide += carbonDioxide_said;

                                        /*土地*/

                                        land_said = weight * 2.5;
                                        land += land_said;
                                        break;
                                    case "11":
                                        /*树*/


                                        /*石油*/
                                        oil_said = weight * 5.7;
                                        oil += oil_said;

                                        /*二氧化碳*/
                                        carbonDioxide_said = weight * 5.5;
                                        carbonDioxide += carbonDioxide_said;

                                        /*土地*/

                                        land_said = weight * 7.6;
                                        land += land_said;
                                        break;
                                    case "12":
                                        /*树*/


                                        /*石油*/
                                        oil_said = weight * 5.7;
                                        oil += oil_said;

                                        /*二氧化碳*/
                                        carbonDioxide_said = weight * 5.5;
                                        carbonDioxide += carbonDioxide_said;

                                        /*土地*/

                                        land_said = weight * 7.6;
                                        land += land_said;
                                        break;
                                    case "13":
                                        /*树*/
                                        tree_said = weight * 17;
                                        tree += tree_said;

                                        /*石油*/
                                        oil_said = weight * 1.2;
                                        oil += oil_said;

                                        /*二氧化碳*/
                                        carbonDioxide_said = weight * 1.52;
                                        carbonDioxide += carbonDioxide_said;

                                        /*土地*/

                                        land_said = weight * 2.5;
                                        land += land_said;
                                        break;
                                    default:
                                        break;
                                }

                                break;

                            }

                        }
                    }

                }


            }



        }
        HashMap<String ,Object> result = new HashMap<>();
        result.put("tree",formatToNumber(new BigDecimal(tree)));
        result.put("oil",formatToNumber(new BigDecimal(oil)));
        result.put("carbonDioxide",formatToNumber(new BigDecimal(carbonDioxide)));
        result.put("land",formatToNumber(new BigDecimal(land)));
        return result;

    }

    /**
     * 计算个人环保值 TODO
     *
     * @param userId
     * @return
     */
    @Override
    public Map<String, Object> ePValueByUserId(String userId) {

        /*计算个人环保值
        *
        * 1 根据用户id查询所有回收记录
        * 2 根据每条回收记录的id查询回收的每种物品
        * 3 记录每种物品的总数
        * 4 每种物品根据提供的计算比例计算每种环保
        * 5 把每种物品的环保相加
        * */

        /*环保值
        *
        * tree 树(课)
        * oil  石油(吨)
        * carbonDioxide 二氧化碳 (吨)
        * land 土地
        * */
        Double tree = new Double(0.0);
        Double oil = new Double(0.0);
        Double carbonDioxide = new Double(0.0);
        Double land = new Double(0.0);

        /*用户的回收记录Id列表*/
        List<RecycleRecord> recycleRecordList = recycleRecordService.getListByUserId(userId);

        /*所有类型*/
        List<Items> itemsList = regenerantService.getType("1");


        for (RecycleRecord recy : recycleRecordList) {

            List<Waste> wastes = wasteService.getWasteByRecyId(recy.getId());

            for (Waste waste : wastes) {
                String typeId = waste.getTypeId();
                Items type = regenerantService.get(typeId);
                Double weight = waste.getNum().doubleValue()/1000;
                Double tree_said ;
                Double oil_said ;
                Double carbonDioxide_said ;
                Double land_said ;


                if (type!=null){
                    for (Items items : itemsList) {
                        if(items.getId().equals(type.getId())){

                            /* 类型  0 PET瓶 1 PE瓶 2 泡沫 3 硬质塑料 4 塑料袋膜 5 织物 6 玻璃
                                    7 复合 8 电子电器 9 书报  10 综合纸 11 金属 12 铝拉罐 13 黄纸板
                             */

                            switch (items.getTyepC()){
                                case "0":

                                    /*树*/
                                     //树不是PET塑料做的

                                    /*石油*/
                                    oil_said = weight*2.3;
                                    oil += oil_said;

                                    /*二氧化碳*/
                                    carbonDioxide_said = weight*2.8;
                                    carbonDioxide += carbonDioxide_said;

                                    /*土地*/
                                     land_said = weight*23.0;
                                    land += land_said;
                                    break;
                                case "1":
                                    /*树*/
                                    //树不是PE塑料做的

                                    /*石油*/
                                    oil_said = weight*2.3;
                                    oil += oil_said;

                                    /*二氧化碳*/
                                    carbonDioxide_said = weight*2.8;
                                    carbonDioxide += carbonDioxide_said;


                                    /*土地*/
                                    land_said = weight*23.0;
                                    land += land_said;

                                    break;
                                case "2":
                                    /*树*/
                                    //树不是泡沫塑料做的

                                    /*石油*/
                                    oil_said = weight*2.3;
                                    oil += oil_said;

                                    /*二氧化碳*/
                                    carbonDioxide_said = weight*2.8;
                                    carbonDioxide += carbonDioxide_said;


                                    /*土地*/
                                    land_said = weight*23.0;
                                    land += land_said;
                                    break;
                                case "3":
                                    /*树*/
                                    //树不是硬质塑料做的

                                    /*石油*/
                                    oil_said = weight*2.3;
                                    oil += oil_said;

                                    /*二氧化碳*/
                                    carbonDioxide_said  = weight*2.8;
                                    carbonDioxide += carbonDioxide_said;


                                    /*土地*/
                                    land_said = weight*23.0;
                                    land += land_said;
                                    break;
                                case "4":

                                    /*树*/
                                    //树不是塑料袋膜做的

                                    /*石油*/
                                    oil_said = weight*2.3;
                                    oil += oil_said;

                                    /*二氧化碳*/
                                    carbonDioxide_said = weight*2.8;
                                    carbonDioxide += carbonDioxide_said;


                                    /*土地*/
                                     land_said = weight*23.0;
                                    land += land_said;



                                    break;
                                case "5":

                                    /*树*/

                                    /*石油*/

                                    /*二氧化碳*/
                                    carbonDioxide_said = weight*5.9;
                                    carbonDioxide += carbonDioxide_said;

                                    /*土地*/

                                    break;
                                case "6":
                                    /*树*/

                                    /*石油*/
                                    oil_said = weight*0.016;
                                    oil += oil_said;

                                    /*二氧化碳*/
                                    carbonDioxide_said = weight*0.0034;
                                    carbonDioxide += carbonDioxide_said;

                                    /*土地*/

                                    land_said = weight*1.53;
                                    land += land_said;

                                    break;
                                case "7":
                                    /*树*/

                                    /*石油*/
                                    oil_said = weight*1.68;
                                    oil += oil_said;

                                    /*二氧化碳*/
                                    carbonDioxide_said = weight*2.04;
                                    carbonDioxide += carbonDioxide_said;

                                    /*土地*/

                                     land_said = weight*1.53;
                                    land += land_said;
                                    break;
                                case "8":
                                    /*树*/
                                    Double tree_avg = 17.0/14.0;
                                    tree_said = weight*tree_avg;
                                    tree += tree_said;
                                    /*石油*/
                                    Double avg_oil = (2.3+0.016+1.68+1.2+5.7)/14;
                                    oil_said = weight*avg_oil;
                                    oil  += oil_said;

                                    /*二氧化碳*/
                                    Double avg_carbonDioxide = (2.8+5.9+0.0034+2.04+1.52+5.5)/14;
                                    carbonDioxide_said = weight*avg_carbonDioxide;
                                    carbonDioxide += carbonDioxide_said;

                                    /*土地*/
                                    Double land_avg = (23+1.53+7.17+2.5+7.6)/14;
                                    land_said = weight*land_avg;
                                    land += land_said;


                                    break;
                                case "9":
                                    /*树*/
                                    tree_said = weight*17;
                                    tree += tree_said;

                                    /*石油*/
                                    oil_said = weight*1.2;
                                    oil += oil_said;

                                    /*二氧化碳*/
                                    carbonDioxide_said = weight*1.52;
                                    carbonDioxide += carbonDioxide_said;

                                    /*土地*/

                                    land_said = weight*2.5;
                                    land += land_said;
                                    break;
                                case "10":
                                    /*树*/
                                    tree_said = weight*17;
                                    tree += tree_said;

                                    /*石油*/
                                    oil_said = weight*1.2;
                                    oil += oil_said;

                                    /*二氧化碳*/
                                    carbonDioxide_said = weight*1.52;
                                    carbonDioxide += carbonDioxide_said;

                                    /*土地*/

                                    land_said = weight*2.5;
                                    land += land_said;
                                    break;
                                case "11":
                                    /*树*/


                                    /*石油*/
                                    oil_said = weight*5.7;
                                    oil += oil_said;

                                    /*二氧化碳*/
                                    carbonDioxide_said = weight*5.5;
                                    carbonDioxide += carbonDioxide_said;

                                    /*土地*/

                                    land_said = weight*7.6;
                                    land += land_said;
                                    break;
                                case "12":
                                    /*树*/


                                    /*石油*/
                                    oil_said = weight*5.7;
                                    oil += oil_said;

                                    /*二氧化碳*/
                                    carbonDioxide_said = weight*5.5;
                                    carbonDioxide += carbonDioxide_said;

                                    /*土地*/

                                    land_said = weight*7.6;
                                    land += land_said;
                                    break;
                                case "13":
                                    /*树*/
                                    tree_said = weight*17;
                                    tree += tree_said;

                                    /*石油*/
                                    oil_said = weight*1.2;
                                    oil += oil_said;

                                    /*二氧化碳*/
                                    carbonDioxide_said = weight*1.52;
                                    carbonDioxide += carbonDioxide_said;

                                    /*土地*/

                                    land_said = weight*2.5;
                                    land += land_said;
                                    break;
                                default:
                                    break;
                            }

                            break;

                        }

                    }
                }

            }


        }
        HashMap<String ,Object> result = new HashMap<>();
        result.put("tree",formatToNumber(new BigDecimal(tree)));
        result.put("oil",formatToNumber(new BigDecimal(oil)));
        result.put("carbonDioxide",formatToNumber(new BigDecimal(carbonDioxide)));
        result.put("land",formatToNumber(new BigDecimal(land)));
        return result;
    }


    @Override
    public Map<String, Object> ePValueAll() {
        /*环保值
         *
         * tree 树(课)
         * oil  石油(吨)
         * carbonDioxide 二氧化碳 (吨)
         * land 土地
         * */
        Double tree = new Double(0.0);
        Double oil = new Double(0.0);
        Double carbonDioxide = new Double(0.0);
        Double land = new Double(0.0);

        /*所有类型*/
        List<Items> itemsList = regenerantService.getType("1");

        List<Map<String, Object>> typeNum = recycleRecordRepository.findListBySql(" select sum(num) as num,typeId as type from waste GROUP BY typeId; ", null);

        for (Map<String, Object> map : typeNum) {

            Double tree_said;
            Double oil_said;
            Double carbonDioxide_said;
            Double land_said;
            for (Items items : itemsList) {

                if (items.getId().equals(map.get("type").toString())){
                    Double num =  Double.parseDouble(map.get("num").toString());
                    Double weight = num/1000;

                    switch (items.getTyepC()){
                        case "0":

                            /*树*/
                            //树不是PET塑料做的

                            /*石油*/
                            oil_said = weight*2.3;
                            oil += oil_said;

                            /*二氧化碳*/
                            carbonDioxide_said = weight*2.8;
                            carbonDioxide += carbonDioxide_said;

                            /*土地*/
                            land_said = weight*23.0;
                            land += land_said;
                            break;
                        case "1":
                            /*树*/
                            //树不是PE塑料做的

                            /*石油*/
                            oil_said = weight*2.3;
                            oil += oil_said;

                            /*二氧化碳*/
                            carbonDioxide_said = weight*2.8;
                            carbonDioxide += carbonDioxide_said;


                            /*土地*/
                            land_said = weight*23.0;
                            land += land_said;

                            break;
                        case "2":
                            /*树*/
                            //树不是泡沫塑料做的

                            /*石油*/
                            oil_said = weight*2.3;
                            oil += oil_said;

                            /*二氧化碳*/
                            carbonDioxide_said = weight*2.8;
                            carbonDioxide += carbonDioxide_said;


                            /*土地*/
                            land_said = weight*23.0;
                            land += land_said;
                            break;
                        case "3":
                            /*树*/
                            //树不是硬质塑料做的

                            /*石油*/
                            oil_said = weight*2.3;
                            oil += oil_said;

                            /*二氧化碳*/
                            carbonDioxide_said  = weight*2.8;
                            carbonDioxide += carbonDioxide_said;


                            /*土地*/
                            land_said = weight*23.0;
                            land += land_said;
                            break;
                        case "4":

                            /*树*/
                            //树不是塑料袋膜做的

                            /*石油*/
                            oil_said = weight*2.3;
                            oil += oil_said;

                            /*二氧化碳*/
                            carbonDioxide_said = weight*2.8;
                            carbonDioxide += carbonDioxide_said;


                            /*土地*/
                            land_said = weight*23.0;
                            land += land_said;



                            break;
                        case "5":

                            /*树*/

                            /*石油*/

                            /*二氧化碳*/
                            carbonDioxide_said = weight*5.9;
                            carbonDioxide += carbonDioxide_said;

                            /*土地*/

                            break;
                        case "6":
                            /*树*/

                            /*石油*/
                            oil_said = weight*0.016;
                            oil += oil_said;

                            /*二氧化碳*/
                            carbonDioxide_said = weight*0.0034;
                            carbonDioxide += carbonDioxide_said;

                            /*土地*/

                            land_said = weight*1.53;
                            land += land_said;

                            break;
                        case "7":
                            /*树*/

                            /*石油*/
                            oil_said = weight*1.68;
                            oil += oil_said;

                            /*二氧化碳*/
                            carbonDioxide_said = weight*2.04;
                            carbonDioxide += carbonDioxide_said;

                            /*土地*/

                            land_said = weight*1.53;
                            land += land_said;
                            break;
                        case "8":
                            /*树*/
                            Double tree_avg = 17.0/14.0;
                            tree_said = weight*tree_avg;
                            tree += tree_said;
                            /*石油*/
                            Double avg_oil = (2.3+0.016+1.68+1.2+5.7)/14;
                            oil_said = weight*avg_oil;
                            oil  += oil_said;

                            /*二氧化碳*/
                            Double avg_carbonDioxide = (2.8+5.9+0.0034+2.04+1.52+5.5)/14;
                            carbonDioxide_said = weight*avg_carbonDioxide;
                            carbonDioxide += carbonDioxide_said;

                            /*土地*/
                            Double land_avg = (23+1.53+7.17+2.5+7.6)/14;
                            land_said = weight*land_avg;
                            land += land_said;


                            break;
                        case "9":
                            /*树*/
                            tree_said = weight*17;
                            tree += tree_said;

                            /*石油*/
                            oil_said = weight*1.2;
                            oil += oil_said;

                            /*二氧化碳*/
                            carbonDioxide_said = weight*1.52;
                            carbonDioxide += carbonDioxide_said;

                            /*土地*/

                            land_said = weight*2.5;
                            land += land_said;
                            break;
                        case "10":
                            /*树*/
                            tree_said = weight*17;
                            tree += tree_said;

                            /*石油*/
                            oil_said = weight*1.2;
                            oil += oil_said;

                            /*二氧化碳*/
                            carbonDioxide_said = weight*1.52;
                            carbonDioxide += carbonDioxide_said;

                            /*土地*/

                            land_said = weight*2.5;
                            land += land_said;
                            break;
                        case "11":
                            /*树*/


                            /*石油*/
                            oil_said = weight*5.7;
                            oil += oil_said;

                            /*二氧化碳*/
                            carbonDioxide_said = weight*5.5;
                            carbonDioxide += carbonDioxide_said;

                            /*土地*/

                            land_said = weight*7.6;
                            land += land_said;
                            break;
                        case "12":
                            /*树*/


                            /*石油*/
                            oil_said = weight*5.7;
                            oil += oil_said;

                            /*二氧化碳*/
                            carbonDioxide_said = weight*5.5;
                            carbonDioxide += carbonDioxide_said;

                            /*土地*/

                            land_said = weight*7.6;
                            land += land_said;
                            break;
                        case "13":
                            /*树*/
                            tree_said = weight*17;
                            tree += tree_said;

                            /*石油*/
                            oil_said = weight*1.2;
                            oil += oil_said;

                            /*二氧化碳*/
                            carbonDioxide_said = weight*1.52;
                            carbonDioxide += carbonDioxide_said;

                            /*土地*/

                            land_said = weight*2.5;
                            land += land_said;
                            break;
                        default:
                            break;
                    }
                    break;
                }


            }


        }
        HashMap<String ,Object> result = new HashMap<>();
        result.put("tree",formatToNumber(new BigDecimal(tree)));
        result.put("oil",formatToNumber(new BigDecimal(oil)));
        result.put("carbonDioxide",formatToNumber(new BigDecimal(carbonDioxide)));
        result.put("land",formatToNumber(new BigDecimal(land)));
        return result;

    }

    public static String formatToNumber(BigDecimal obj) {
        DecimalFormat df = new DecimalFormat("#.000");
        if(obj.compareTo(BigDecimal.ZERO)==0) {
            return "0.00";
        }else if(obj.compareTo(BigDecimal.ZERO)>0&&obj.compareTo(new BigDecimal(1))<0){
            return "0"+df.format(obj).toString();
        }else {
            return df.format(obj).toString();
        }
    }
}
